package com.ujcms.cms.core;

import com.ujcms.cms.core.web.filter.CorsFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 10100
 * @license (C) Copyright 2020-2050,  Corporation Limited.
 * @contact wang.he@cpe-smartcity.com
 * @date 2021/5/6 20:44
 * @description 对options 请求处理 //@Configuration
 */
@Configuration
public class BaseFilterConfig {

    @Value("${cors-config.use-gateway:false}")
    private boolean useGateway;

    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilterBean() {
        //通过FilterRegistrationBean实例设置优先级可以生效
        //通过@WebFilter无效
        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>();
        //注册自定义过滤器
        bean.setFilter(new CorsFilter(useGateway));
        //过滤器名称
        bean.setName("corsFilter");
        //过滤所有路径
        bean.addUrlPatterns("/*");
        //优先级，越低越优先
        bean.setOrder(-1111);
        return bean;
    }
}